layui.use(['table', 'plugins'], function () {
    var extend = {
        index_url: 'index',
        add_url: 'add',
        edit_url: 'edit',
        del_url: 'delete'
    };

    var table = layui.table;
    var plugins = layui.plugins;

    table.render(plugins.tableOption({
        url: extend.index_url,
        cols: [[
            {field: 'id', title: 'ID', align: 'center', width: 100},
            {field: 'pid', title: '父级', align: 'left'},
            {field: 'name', title: '角色名称', align: 'left'},
            {field: 'remark', title: '备注', align: 'center'},
            {field: 'status', title: '状态', align: 'center', templet: function (d) {
                return d.status === 1 ? '<span style="color:green">正常</span>' : '<span style="color:red">禁用</span>';
            }},
            {fixed: 'right', title: '操作', toolbar: '#bar', width: 150, align: 'center'}
        ]]
    }));

    //监听行工具事件
    table.on('tool(buttonClick)', function (obj) {
        if (obj.event === 'delete') {
            layer.confirm('您确定要 <span style="color:#f56954">删除</span> 吗？', function (index) {
                backend.api.ajax({ // 删除
                    url: extend.del_url, rid: obj.data.id
                }, extend.index_url, 'DELETE');
            });
        } else if (obj.event === 'edit') { //编辑
            backend.api.ajax({
                url: extend.edit_url, rid: obj.data.id
            }, extend.index_url);
        }
    });
});